﻿using System.Net.Sockets;
using System.Threading;

namespace PersonEntity
{
    public class DS_NET_Server 
    {
        TcpListener serverSocket;
        int port = 4040;

        iDS DS = null;
        public Thread ListenThread;
        Socket clientSocket;

        public void Start(iDS ds)
        {
            this.DS = ds;
            serverSocket = new TcpListener(port);            
           
            //clientSocket = default(TcpClient);
            serverSocket.Start();
            ListenThread = new Thread(Listen);
            ListenThread.Start();            
        }

        public void Stop()
        {
                ListenThread.Abort();
                if (clientSocket != null)
                {
                    clientSocket.Close();
                }
                serverSocket.Server.Close();
                serverSocket.Stop();                
        }

        private void Listen()
        {
            while ((true))
            {
                
                clientSocket = serverSocket.AcceptSocket();
                
                //TcpClient ttt = serverSocket.
                handleClinet client = new handleClinet();
                client.Server = this;
                client.startClient(clientSocket, DS);
            }
        }
    }
}
